﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using MVC.Web.UI.Attributes;

namespace MVC.Web.UI.Tabs
{
    /*
     * cookies needs to be implemented on the next iteration
     */ 
    public class WebTab : MvcControl
    {
        #region Fields
    
        private Dictionary<TabEvents, string> _tabEventHandlers; 
        private WebTabCollection<NavigationItem> _tabs;
        private HtmlHelper _htmlHelper;
                 
        #endregion

        #region Constructors

        public WebTab(HtmlHelper htmlHelper)
            : base("div", htmlHelper.ViewContext)
        {
        } 
        #endregion

        #region Properties
        public string  Scripts { get; set; }    

        public Dictionary<TabEvents, string> TabEventHandlers
        {
            get {
                    if (this._tabEventHandlers == null)
                        return this._tabEventHandlers = new Dictionary<TabEvents, string>();
                    return this._tabEventHandlers; 
                    
                }
        } 

        public  WebTabCollection<NavigationItem> TabItems
        {
            get
            {
                if (this._tabs == null)
                    return this._tabs = new WebTabCollection<NavigationItem>();
                return _tabs;
            }
            set { _tabs = value; }
        }

        public HtmlHelper HtmlHelper
        {
            get { return _htmlHelper; }
            set { _htmlHelper = value; }
        }

        public Action Items { get; set; }
             
        public int Selected
        {
            get;
            set;
        }

        public bool Cache
        {
            get;
            set;
        }

        public bool Collapsible
        {
            get;
            set;
        }


        public string Spinner
        {
            get;
            set;
        }

        public string TabIdPrefix
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }


        public bool AjaxOptions
        {
            get;
            set;
        }
        public int[] Disabled
        {
            get;
            set;
        }
        public bool Deselectable
        {
            get;
            set;
        }
        public string Event 
        {
            get;
            set;
        }
        public string IdPrefix
        {
            get;
            set;
        }
        public string PanelTemplate
        {
            get;
            set;
        }

        public string TabTemplate
        {
            get;
            set;
        }

        public bool Disable
        {
            get;
            set;
        }
     
        #endregion Methods 
       
        #region Overrides

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            //Render Navigation for each tab    
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            foreach (Tab item in this.TabItems)
            {
                if (item.IsAjaxLoad != true)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    //A
                    writer.AddAttribute("href", "#" + item.TabId);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.InnerWriter.Write(item.Name);
                    writer.RenderEndTag();//end A 
                    writer.RenderEndTag();//end LI    
                }
                else 
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    //A
                    writer.AddAttribute("href", item.Url);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.InnerWriter.Write(item.Name);
                    writer.RenderEndTag();//end A 
                    writer.RenderEndTag();//end LI    
                }
            }
            //end Ul
            writer.RenderEndTag();

            //Render the content of each tab
            //If the tab will load its data via AJAX no div /content/ is rendered
            foreach (Tab tab in this.TabItems)
            {
                if (tab.IsAjaxLoad != true)
                {
                    //Render the content of each tab 
                    writer.AddAttribute("id", tab.TabId);
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    if (tab.Content != null)
                    {
                        tab.Content.WriteTo(writer);
                    }
                    writer.RenderEndTag();//end div 
                }
            }  
        }

        protected override void RenderScripts(HtmlTextWriter writer)
        {
           writer.Write(this.Scripts);
        }

        public override void RenderControl(ViewContext context)
        {
            base.RenderControl(context);
        }

        #endregion


    } 
}
